package com.googlecode.jmicroerp.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;


/**
 *
 * @author ionel.david
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "apometre")

@NamedQueries({
    @NamedQuery(
            name = "Apometre.queryByRecipientAll",
            query = "select apm.dataCitirii,apm.valoare "
            + "from Apometre apm "
            + "where apm.recipient.id = ?1 "
            + "and apm.dataCitirii = ?2 "),
    @NamedQuery(
            name = "Apometre.queryApometre",
            query = "select apm.recipient.denumire, apm.dataCitirii, apm.valoare, apm.recipient.id, apm.recipient.magazin.id, apm.recipient.stoc.produs.id "
            + "from Apometre apm "
            + "where apm.recipient.id = ?1 "
            //+ "and apm.dataCitirii = ?2 "
            + "order by apm.dataCitirii desc"),
     @NamedQuery(
             name = "Apometre.lastIndexByRecipient",
             query = "select apm.valoare "
             + "from Apometre apm "
             + "where apm.recipient.id = ?1 "
             + "order by dataCitirii desc ")            
})

/* query.setMaxResults(1) */
public class Apometre extends AbstractEntitate {

    private Recipient recipient;
    private Date dataCitirii;
    private BigDecimal valoare;
    private Boolean monitorizeaza;
    private String dataFormatRo;


    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Recipient.class)
    @JoinColumn(name = "RECIPIENT_ID")
    public Recipient getRecipient() {
        return recipient;
    }

    public void setRecipient(Recipient recipient) {
        this.recipient = recipient;
    }

    @Column(name = "DATA_CITIRII")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getDataCitirii() {
        return dataCitirii;
    }

    public void setDataCitirii(Date dataCitirii) {
        this.dataCitirii = dataCitirii;
        this.dataFormatRo = new SimpleDateFormat("dd.MM.yyyy").format(this.dataCitirii);
    }

    @Column(name = "VALOARE")
    public BigDecimal getValoare() {
        return valoare;
    }

    public void setValoare(BigDecimal valoare) {
        this.valoare = valoare;
    }

    @Column(name = "MONITORIZEAZA")
    public Boolean getMonitorizeaza() {
        return monitorizeaza;
    }

    public void setMonitorizeaza(Boolean monitorizeaza) {
        this.monitorizeaza = monitorizeaza;
    }

    @Transient
	public String getDataFormatRo() {
		return dataFormatRo;
	}

	public void setDataFormatRo(String dataFormatRo) {
		this.dataFormatRo = dataFormatRo;
	}

}
